package com.xingkeduo.delayQueue.model;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author chi  2018-08-01 12:53
 **/
@Data
@AllArgsConstructor
public class Task implements Delayed {
    //业务id
    private String id;
    // 任务
    private String value ;
    // 任务过期时间

    private long time;


    @Override
    public long getDelay(TimeUnit unit) {
        long convert = unit.convert(time - System.currentTimeMillis(), TimeUnit.NANOSECONDS);
        return convert;
    }

    @Override
    public int compareTo(Delayed o) {
        Task o1 = (Task) o;
        return (int)(this.time - o1.getTime());
    }

    public Task(String id, String value) {
        this.id = id;
        this.value = value;
    }
}
